package fr.cnam.projetSiw.server.lastFm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.umass.lastfm.Chart;
import de.umass.lastfm.Event;
import de.umass.lastfm.Geo;
import de.umass.lastfm.ImageSize;
import de.umass.lastfm.PaginatedResult;
import de.umass.lastfm.Venue;
import fr.cnam.projetSiw.client.services.LastFmService;
import fr.cnam.projetSiw.shared.Artist;
import fr.cnam.projetSiw.shared.City;
import fr.cnam.projetSiw.shared.MusicEvent;
import fr.cnam.projetSiw.shared.Track;

@SuppressWarnings("serial")
public class LastFmServiceImpl extends RemoteServiceServlet implements LastFmService {
	
    private String key  = "97ceac20c116f9d4b4c4e4b83563049b";
//    private String userName = "Polegar22";
	
    @Deprecated
	@Override
	public ArrayList<Artist> getWeeklyArtistChart()
	{
		ArrayList<Artist> results = new ArrayList<Artist>();
		PaginatedResult<de.umass.lastfm.Artist> chart = Chart.getTopArtists(key);
	    Collection<de.umass.lastfm.Artist> artists = chart.getPageResults();
	    for (de.umass.lastfm.Artist artist : artists)
	    {
	    	results.add(new Artist(artist.getName(), artist.getTags(),artist.getImageURL(ImageSize.EXTRALARGE)));
	    }
	    return results;
	}
	
	@Override
	public ArrayList<Artist> getHypeArtistByCity(City city){
		ArrayList<Artist> results = new ArrayList<Artist>();
		Chart<de.umass.lastfm.Artist> geo = Geo.getMetroHypeArtistChart(city.getCountryName(), city.getName(), key);
	    Collection<de.umass.lastfm.Artist> artists = geo.getEntries();
	    for (de.umass.lastfm.Artist artist : artists)
	    {
	    	try{
	    		results.add(getArtistByName(artist.getName()));
	    	}
	    	catch(NumberFormatException e){
	    		
	    	}
	    }
	    return results;
	}

	@Override
	public ArrayList<MusicEvent> getEventsByLatAndLon(double lat, double lon) 
	{
		ArrayList<MusicEvent> events = new ArrayList<MusicEvent>();
		PaginatedResult<Event> geo = Geo.getEvents(lat, lon, 1, key);
		Collection<Event> geoEvent = geo.getPageResults();
		for(Event event : geoEvent)
		{
			MusicEvent musicEvent = new MusicEvent();
			musicEvent.setName(event.getTitle());
			musicEvent.setDescription(event.getDescription());
			musicEvent.setWebSite(event.getWebsite());
			musicEvent.setEventImg(event.getImageURL(ImageSize.EXTRALARGE));
			ArrayList<Artist> lstGuest = new ArrayList<Artist>();
			for(String guest : event.getArtists())
			{
				try{
					lstGuest.add(getArtistByName(guest, ImageSize.SMALL));
				}
				catch(NumberFormatException e){
					
				}
			}
			musicEvent.setAssociatedArtist(lstGuest);
			musicEvent.setNbParticipant(event.getAttendance());
			Venue venue = event.getVenue();
			if(venue != null)
			{
				musicEvent.setEventLocationName(venue.getName());
				musicEvent.setCity(new City(venue.getCity(), venue.getCountry()));
			}
			
			events.add(musicEvent);
		}
		Collections.sort(events,new Comparator<MusicEvent>()
		{

			@Override
			public int compare(MusicEvent o1, MusicEvent o2)
			{
				if(o1 != null && o2 != null)
				{
					if(o1.getNbParticipant() < o2.getNbParticipant())
					{
						return 1;
					}
					return -1;
				}
				return 0;
			}
		});
		return events;
	}
	
	@Override
	public Track getTrackByName(String artistName, String trackName){
		
		if(artistName == null || artistName.isEmpty())
			return new Track();
		if (trackName == null || trackName.isEmpty())
			return new Track();
		Track track = null;
		try
		{
			de.umass.lastfm.Track fmTrack = de.umass.lastfm.Track.getInfo(artistName, trackName, key);
			

			if(fmTrack != null){
				track = new Track();
				track.setArtist(getArtistByName(artistName, ImageSize.LARGE));
				track.setName(trackName);
				track.setListeners(fmTrack.getListeners());
				track.setAlbumName(fmTrack.getAlbum());
				track.setImgUrl(fmTrack.getImageURL(ImageSize.EXTRALARGE));
			}
		}
		catch (Exception e) {
			return new Track();
		}
			
		return track;
		

	}
	
	public Artist getArtistByName(String name)
	{
		return getArtistByName(name, ImageSize.LARGE);
	}

	public Artist getArtistByName(String name, ImageSize imgSize)
	{
		if (name == null || name.trim().isEmpty())
			return new Artist();
		
		Artist artist = null;

		try{
			
			de.umass.lastfm.Artist fmArt = de.umass.lastfm.Artist.getInfo(name, key);
	
			if(fmArt != null){
				artist = new Artist();
				artist.setName(fmArt.getName());
				artist.setImg(fmArt.getImageURL(imgSize));
				artist.setGenres(fmArt.getTags());
				artist.setPlays(fmArt.getPlaycount());
				artist.setDescription(fmArt.getWikiText());
			}
		}
 catch (Exception e)
		{
			return new Artist();
		}
		return artist;

	}
	
	

}
